define(['Framework7'], function (Framework7) {

    window.pageCtrl = {
        //localUserId : localStorage.getItem("user_id"),
        loginPage: function () {
            window.localUserId = window.localUserId?window.localUserId:localStorage.getItem("user_id");
            window.accessToken = window.accessToken?window.accessToken:localStorage.getItem("access_token");
            window.localToken = window.localToken?window.localToken:localStorage.getItem("user_token");
            window.loginTime = window.loginTime?window.loginTime:localStorage.getItem("login_time");
            if(localUserId&&localToken&&loginTime){//token登录
                pageCtrl.loginWithToken();
            }else{
                    yunSpaceApp.showTab('#mainView');
                    if($("[data-page='login']").length > 0){
                        mainView.router.back('login.html');

                    }else{
                        mainView.router.loadPage('login.html');
                    }
            }
        },
        orderInfo:function(query){
            yunSpaceApp.showIndicator();
            var url =appMain.apiRoot+'vendor/order_info';
            $$.ajax({
                url:url,
                type:'post',
                dataType:'json',
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'order_id':query.id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success: function (data) {
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data){
                        setTimeout(function () {
                            var html = templates.showOrderInfoTemplate({shows:data});
                            $$('#orderInfo').html(html);
                            $('#confirmOrder').bind('click', function () {
                                pageCtrl.confirmOrder(query.id)
                            });
                            $('#cancelOrder').bind('click', function () {
                                pageCtrl.cancelOrder(query.id);
                            });
                            yunSpaceApp.hideIndicator();
                        },400)

                    }else{
                        yunSpaceApp.alert('获取信息失败！！','提示');
                        yunSpaceApp.hideIndicator();
                    }
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        confirmOrder:function(order_id){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+'vendor/vendorHandleOrder';
            $$.ajax({
                url:url,
                type:'post',
                dataType:'json',
                timeout:requestTimeOut,
                data:{
                    'id':order_id,
                    'user_id':localUserId,
                    'status':1,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success: function (data) {
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.status == 1){
                        yunSpaceApp.alert('订单已通过','提示')
                    }
                    if(!data.status){
                        yunSpaceApp.alert('订单处理失败！请重试','提示')
                    }
                    if(data.status == 3){
                        yunSpaceApp.alert('操作异常请稍后再试','提示')
                    }
                    mainView.router.reloadPage('orderInfo.html?id='+order_id+'&deal=confirm')

                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        cancelOrder : function (order_id) {
            yunSpaceApp.confirm('确认放弃订单吗！',
                '提示',function(){
                    yunSpaceApp.showIndicator();
                    var url = appMain.apiRoot+'vendor/vendorHandleOrder';
                    $$.ajax({
                        url:url,
                        type:'post',
                        dataType:'json',
                        timeout:requestTimeOut,
                        data:{
                            'id':order_id,
                            'user_id':localUserId,
                            'status':2,
                            'access_token':accessToken,
                            'login_time':loginTime
                        },
                        success: function (data) {
                            if(!pageCtrl.checkAccess(data)){
                                return;
                            };
                            if(data.status == 1){
                                yunSpaceApp.alert('订单已已放弃','提示')
                            }
                            if(!data.status){
                                yunSpaceApp.alert('订单处理失败！请重试','提示')
                            }
                            if(data.status == 3){
                                yunSpaceApp.alert('操作异常请稍后再试','提示')
                            }
                            mainView.router.reloadPage('orderInfo.html?id='+order_id+'&deal=cancel')
                            yunSpaceApp.hideIndicator();
                        },
                        error: function (XHR, TS, ET) {
                            appMain.networkError('',XHR,TS,ET);
                        }
                    })
                },
                function(){}

            )
        },
        orderList : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+'vendor/user_order_list';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                   if(!pageCtrl.checkAccess(data)){
                       return;
                   };
                    setTimeout(function(){
                        if(data.length > 0){
                            $('#moreOrderList').show();
                        }
                        var html = templates.showOrderTemplate({shows:data});
                        yunSpaceApp.hideIndicator();
                        $('#orderList').html(html);
                        if(data.length > 0){
                            $('#order .no_content').hide();
                        }
                        window.ptrContentOrder = $$('#order .pull-to-refresh-content');
                        ptrContentOrder.off('refresh');
                        ptrContentOrder.on('refresh',function(){
                            pageCtrl.orderListRefresh();
                        });
                        $('#moreOrderList').off('click');
                        $('#moreOrderList').on('click', function (){
                            pageCtrl.loadMoreOrderList();
                        });
                    },350)
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        checkAccess : function (data) {
            if(status in data){
                if(data.status != -10){
                    return true
                }
                yunSpaceApp.alert(data.info,'提示');
                //pageCtrl.logOut();
                yunSpaceApp.hideIndicator();
                return false;
            }else{
                return true;
            }
        },
        loginWithToken:function(){
            yunSpaceApp.showIndicator();
            var code= '';
            var url = appMain.apiRoot+ 'vendor/login_with_token';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'token':localToken,
                    'login_time':loginTime
                },
                success:function(data){
                    status = data.status;
                    if(status == -2){
                        window.localToken = '';
                        localStorage.removeItem('user_token');
                        pageCtrl.loginPage();
                        yunSpaceApp.hideIndicator();
                        return
                    }
                    if(status == 1){//
                        $('.myName').text(data.username);
                        $('.myAccount span').text(data.fullname);
                        mainView.router.loadPage('order.html');
                        $('#user_info img').attr('src',data.avatar);
                        yunSpaceApp.params.swipePanel = 'left';
                    };
                    if(status == -2||status == -1){
                        setTimeout(function () {
                            mainView.router.loadPage('login.html');
                        },500)
                    };
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            });

        },
        orderListRefresh:function(){
            var url =appMain.apiRoot+ 'vendor/user_order_list';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var html = templates.showOrderTemplate({shows:data});
                    setTimeout(function(){
                        if(data.length > 0){
                            $('#moreOrderList').show();
                        }
                        yunSpaceApp.hideIndicator();
                        $$('#orderList').html(html);
                        if(data.length > 0){
                            $('#order .no_content').hide();
                        }
                    },100)
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        logOut:function(){
            //console.log('logout');
            localStorage.removeItem('login_time');
            localStorage.removeItem('user_id');
            localStorage.removeItem('user_token');
            localStorage.removeItem('access_token');

            window.localUserId = '';
            window.accessToken = '';
            window.localToken = '';
            window.loginTime = '';

            $('#orderList').html('');
            $('#contactList').html('');
            $('#serviceList').html('');
            $('#myPurse').html('');

            yunSpaceApp.closePanel();
            pageCtrl.loginPage();
            yunSpaceApp.showTab('#mainView');

            yunSpaceApp.params.swipePanel = false;
        },
        personInfo:function(){
            yunSpaceApp.showIndicator();
            yunSpaceApp.closePanel();
            yunSpaceApp.showTab('#mainView');
            var localUserId =localStorage.getItem("user_id");
            var url =appMain.apiRoot+ 'vendor/user_info';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var html = templates.showPersonInfoTemplate({balance:data.balance,orders:data.orders});
                    $('#person').html(html);
                    var headImg = $('#user_info img').attr('src');
                    var name = $('#user_info .myName').text();
                    $('.personTop .myName').text(name);
                    $('.personTop .img img').attr('src',headImg);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })

        },
        loadMoreOrderList : function(){
            yunSpaceApp.showIndicator();
            var localUserId =localStorage.getItem("user_id");
            var last_id = $('#orderList a:last').data('id');
            var url = appMain.apiRoot+ 'vendor/load_more_order_list';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'last_id':last_id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(!data.length){
                        yunSpaceApp.alert('没有更多订单信息','提示');
                    }
                    var html = templates.showOrderTemplate({shows:data});
                    $('#orderList').append(html);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        serviceList : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_service_list';
            var localUserId =localStorage.getItem("user_id");
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.length > 0){
                        $('#moreServiceList').show();
                    }
                    var html = templates.showServiceTemplate({shows:data});
                    $('#serviceList').html(html);
                    window.serviceContent = $$('#service .pull-to-refresh-content');
                    serviceContent.on('refresh',function(){
                        pageCtrl.serviceListRefresh()
                    });
                    $('#moreServiceList').off('click');
                    $('#moreServiceList').on('click', function () {
                        pageCtrl.moreServiceList();
                    });
                    if(data.length > 0){
                        $('#service .no_content').hide();
                    }
                    yunSpaceApp.hideIndicator();
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        serviceListRefresh : function () {
            var url = appMain.apiRoot+ 'vendor/get_service_list';
            var localUserId =localStorage.getItem("user_id");
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.length > 0){
                        $('#moreServiceList').show();
                    }
                    var html = templates.showServiceTemplate({shows:data});
                    $('#serviceList').html(html);
                    if(data.length > 0){
                        $('#service .no_content').hide();
                    }
                    yunSpaceApp.hideIndicator();
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        serviceInfo: function (query) {
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_service_info';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'service_id':query.id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    console.log(data)
                    //console.log(JSON.stringify(data));
                    setTimeout(function(){
                        var html = templates.showServiceInfoTemplate({shows:data});
                        $('#serviceInfo').html(html);
                        var mySwiper = new Swiper('.swiper-container', {
                            //preloadImages: false,
                            //lazyLoading: true,
                            //loop:true,
                            autoplay:3000,
                            effect:'slide',
                            fade: {
                                crossFade: true
                            },
                            pagination: '.swiper-pagination'
                        })
                        yunSpaceApp.hideIndicator();
                    },400)

                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        moreServiceList : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_service_list';
            var last_id = $('#serviceList li').last().data('id');
            //console.log(last_id);
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'last_id':last_id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.length){
                        var html = templates.showServiceTemplate({shows:data});
                        $('#serviceList').append(html);
                    }else{
                        yunSpaceApp.alert('没有更多服务内容','提示');
                    }
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        getContacts : function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_contacts';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var html = templates.showContactTemplate({shows:data});
                    $('#contactList').html(html);
                    $('#moreContactList').on('click',function(){
                        pageCtrl.moreContacts();
                    });
                    window.contactsContent = $$('#messagePage .pull-to-refresh-content');
                    contactsContent.on('refresh',function(){
                        pageCtrl.contactsListRefresh();
                    });
                    if(data.length > 0){
                        $('#messagePage .no_content').hide();
                    }
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        contactsListRefresh : function () {
            var url = appMain.apiRoot+ 'vendor/get_contacts';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var html = templates.showContactTemplate({shows:data});
                    $('#contactList').html(html);
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        moreContacts : function (){
            var last_id = $('#contactList li').last().data('time');
            //console.log(first_id);
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_contacts';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'last_id':last_id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    var html = templates.showContactTemplate({shows:data});
                    console.log(data);
                    $('#contactList').append(html);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        getMessageContent: function (from_id) {
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_messages_content';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'from_id':from_id,
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var showTimeTip =new Array();
                    for(var i = 0; i<data.length;i++){
                        //data[i]['send_format_time']
                        var s = showTimeTip.indexOf(data[i]['send_format_time']);
                        if(s >=0){
                            delete data[i]['send_format_time'];
                        }else{
                            showTimeTip.push(data[i]['send_format_time']);
                        }
                    }
                    setTimeout(function () {
                        var html = templates.showMessageContentTemplate({shows:data});
                        //console.log(query.from_id);
                        $('#messagesList .messages').html(html);
                        $('#messagesList').scrollTop(99999);
                    },400)
                    clearInterval(pageCtrl.messageInterval);//清除获取消息的计时器，计时器带有不同的from_id
                    pageCtrl.messageInit(from_id);
                    pageCtrl.messageHeartBeat(from_id);
                    $('#moreMessageList').on('click',function(){
                        pageCtrl.moreMessageList(from_id);
                    });
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        messageInit:function(oppo_id){
            pageCtrl.conversationStarted = false;

            // Init Messages
            pageCtrl.myMessages = yunSpaceApp.messages('.messages', {
                autoLayout:true
                //messageTemplate:showOrderInfoTemplate
            });

            // Init Messagebar
            pageCtrl.myMessagebar = yunSpaceApp.messagebar('.messagebar');

            // Handle message
            $$('.messagebar .link').on('click', function () {
                // Message text
                var messageText = pageCtrl.myMessagebar.value().trim();
                // Exit if empy message
                if (messageText.length === 0) return;

                // Empty messagebar
                pageCtrl.myMessagebar.clear()
                pageCtrl.sendMessage(oppo_id,messageText);

                // 更新会话flag

                pageCtrl.conversationStarted = true;
            });
        },
        sendOneTime : '',
        sendMessage: function (to_id,message) {
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/send_message';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'to_id':to_id,
                    'user_id':localUserId,
                    'message':message,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    var tenMin =  10*60,text;
                    if(data){
                        var timestamp = Date.parse(new Date());
                        timestamp = timestamp/1000;
                        if(!pageCtrl.sendOneTime){
                            pageCtrl.sendOneTime = data.send_time;
                            text = '<div class="messages-date">'+data.send_my_time+'</span></div>'
                        }else{
                            if(data.send_time - pageCtrl.sendOneTime <= tenMin){
                                text = '';
                            }else{
                                pageCtrl.sendOneTime = data.send_time;
                                text = '<div class="messages-date">'+data.send_my_time+'</span></div>'
                            }
                        }

                        text = text + '<div class="message message-sent message-with-avatar message-last message-with-tail message-first" data-id="'+data.id+'">'
                            + '<div class="message-text">'+data.message+'</div>'
                            + '<div style="" class="message-avatar"></div>'
                            + '<div style="background-image:url('+data.avatar+')" class="message-avatar"></div>'
                            + '</div>';

                        $('#messagesList>.messages').append(text);
                        $('#messagesList').scrollTop(99999);

                    }else{
                        yunSpaceApp.alert('发送失败！！','提示');
                    }
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    yunSpaceApp.alert('发送失败！！','提示');
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        messageHeartBeat : function(from_id){
            pageCtrl.messageInterval = setInterval(function(){
                var lastMessageId = $('#messagesList div.message').last().data('id');
                if(lastMessageId){
                    var url = appMain.apiRoot+ 'vendor/get_message_by_heart_beat';
                    $$.ajax({
                        type:'post',
                        dataType:'json',
                        url:url,
                        cache:false,
                        data:{
                            'from_id':from_id,
                            'user_id':localUserId,
                            'last_id':lastMessageId,
                            'access_token':accessToken,
                            'login_time':loginTime
                        },
                        success:function(data){
                            if(data.length){
                                var html = templates.showMessageContentTemplate({shows:data});
                                $('#messagesList>.messages').append(html);
                                $('#messagesList').scrollTop(99999);
                            }
                            //console.log(data.length);

                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.status);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
                    })
                }else{
                    clearInterval(pageCtrl.messageInterval)
                }
            },4000)

        },
        moreMessageList :function(from_id){
            var firstId = $('#messagesList .messages div.message').first().data('id');
            var url = appMain.apiRoot+ 'vendor/get_messages_content';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'from_id':from_id,
                    'user_id':localUserId,
                    'first_id':firstId,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    var showTimeTip =new Array();
                    for(var i = 0; i<data.length;i++){
                        //data[i]['send_format_time']
                        var s = showTimeTip.indexOf(data[i]['send_format_time']);
                        if(s >=0){
                            delete data[i]['send_format_time'];
                        }else{
                            showTimeTip.push(data[i]['send_format_time']);
                        }
                    }

                    if(!data.length){
                        yunSpaceApp.alert('没有更多消息','提示');
                    }else{
                        var html = templates.showMessageContentTemplate({shows:data});
                        $('#messagesList>.messages').prepend(html);
                    }

                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    //appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        pursePage:function(){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_user_admin_money';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime,
                    //'page':
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.status == -2){
                        data.data = !!data.data
                    }else{
                        data.data.account_detail_length = data.data.account_detail.length;
                    }

                    var html = templates.showMyPurseTemplate({shows:data.data});
                    $('#myPurse').html(html);
                    yunSpaceApp.hideIndicator();
                    yunSpaceApp.pullToRefreshDone();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }

            })
        },
        moreMoneyList : function (thisDom) {
            //$(thisDom).find('a').data('pagenumber',3)
            var pageNumber = $(thisDom).find('a').data('pagenumber');
            //console.log(pageNumber);
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_account_detail_load_more';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'access_token':accessToken,
                    'login_time':loginTime,
                    'page':pageNumber
                },
                success:function(data){
                    if(!pageCtrl.checkAccess(data)){
                        return;
                    };
                    if(data.status == -2){
                        data.data = !!data.data
                    }else{
                        data.data.account_detail_length = data.data.account_detail.length;
                    }
                    var html = templates.showMyMoneyListTemplate({shows:data.data});
                    $('#moneyList').append(html);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                    yunSpaceApp.pullToRefreshDone();
                }
            })
        },
        getCode: function () {
            yunSpaceApp.showIndicator();
            var phone =$.trim($('#phone').val());
            if(!phone){
                yunSpaceApp.alert('请输入手机号！','提示');
                yunSpaceApp.hideIndicator();
                return false;
            }
            var url = appMain.apiRoot+ 'vendor/register_send_sms';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'phone':phone
                },
                success:function(data){
                    console.log(data);
                    if(data.status==1){
                        localStorage.setItem("code",data.data.code);
                        localStorage.setItem("register_token",data.data.token);
                        pageCtrl.getCodeBtnInterval();
                        yunSpaceApp.alert('验证码已发送！','提示');
                    }else{
                        yunSpaceApp.alert(data.info,'提示');
                    }
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            });

        },
        getCodeBtnInterval :function(){
            var min = 60;
            $('#getCode').attr('disabled',true);
            var text = min+'秒';
            $('#getCode').text(text);
            var getCodeInterval = setInterval(function () {
                min = min -1;
                text = min+'秒';
                if(min <= 0){
                    $('#getCode').text('短信验证');
                    $('#getCode').removeAttr('disabled');
                    clearInterval(getCodeInterval);
                }else{
                    $('#getCode').text(text)
                }

            },1000)
        },
        register1Submit:function(){
            yunSpaceApp.showIndicator();
            var phone =$.trim($('#phone').val());
            var code = $.trim($('#registerCode').val())
            if(!code||!phone){
                yunSpaceApp.alert('请输入验证码！','提示');
                yunSpaceApp.hideIndicator();
                return false;
            }
            var register_token = localStorage.getItem('register_token');
            var url = appMain.apiRoot+ 'vendor/check_phone_code';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'phone':phone,
                    'token':register_token,
                    'code':code
                },
                success:function(data){
                    console.log(data);
                    if(data.status==1){
                        localStorage.setItem("register_phone",phone);
                        mainView.loadPage('register_2.html');
                    }else{
                        yunSpaceApp.alert('验证失败！','提示');
                    }
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            });
            return false;
        },
        register2Submit:function(){
            yunSpaceApp.showIndicator();
            var username = $.trim($('#registerUsername').val());
            var password = $.trim($('#registerPassword').val());
            var confirmPassword = $.trim($('#registerConfirmPassword').val());
            var phone = localStorage.getItem("register_phone");
            if(!username||!password){
                yunSpaceApp.alert('用户名和密码不能为空！','提示');
                yunSpaceApp.hideIndicator();
                return false;
            }
            if(password!=confirmPassword){
                yunSpaceApp.alert('两次输入的密码不一样！','提示');
                yunSpaceApp.hideIndicator();
                return false;
            }
            var url = appMain.apiRoot+ 'vendor/register_submit';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'username':username,
                    'phone':phone,
                    'password':password,
                    'rpassword':confirmPassword
                },
                success:function(data){
                    console.log(data);
                    if(data.status==1){
                        yunSpaceApp.alert(data.info,'提示');
                        appMain.login(username,password);
                    }else{
                        yunSpaceApp.alert(data.info,'提示');

                    }
                    //if(data.status==1){
                    //    localStorage.setItem("register_phone",phone);
                    //    mainView.loadPage('register_2.html');
                    //}
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    yunSpaceApp.alert('注册失败！','提示');
                    appMain.networkError('',XHR,TS,ET);
                }
            });
            return false;
        },
        purseChangeDate:function(date){
            yunSpaceApp.showIndicator();
            var url = appMain.apiRoot+ 'vendor/get_user_admin_money';
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'date':date,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    var account_detail_length = data.data.account_detail.length;
                    var html = templates.showMyMoneyListTemplate({account_detail:data.data.account_detail,account_detail_length:account_detail_length});
                    $('#moneyList').html(html);
                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })
        },
        product_id:"",
        getSheduleData:function(product_id,product_name){
            pageCtrl.product_id = product_id;
            yunSpaceApp.showIndicator();
            var calendarDateFormat = yunSpaceApp.calendar({
                input: '#calendar-date-format1',
                dateFormat:'yyyy-MM-dd',
                monthNames:['01', '02', '03', '04', '05', '06', '07', '08' , '09' , '10', '11', '12'],
                dayNamesShort:['日', '一', '二', '三', '四', '五', '六']
            });
            var calendarDateFormat = yunSpaceApp.calendar({
                input: '#calendar-date-format2',
                dateFormat: 'yyyy-MM-dd',
                monthNames:['01', '02', '03', '04', '05', '06', '07', '08' , '09' , '10', '11', '12'],
                dayNamesShort:['日', '一', '二', '三', '四', '五', '六']
            });
            var url = appMain.apiRoot+ 'vendor/getProductScheduleOne';
            var color='';
            $('#add-shedule').on('click',function(){
                pageCtrl.addSheduleData(product_id,product_name)
            });
            $(".color").click(function(){
                    $(".color").css({
                        border:"0px"
                    });
                    $(this).css({
                        border:"2px solid #cddea5"
                    })
                    color=$(this).data("text");
                    $("#shedule-color").val(color);
            });
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'product_id':product_id,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    console.log(data);
                    data['color']=color;
                    if(data.status !=1){
                        yunSpaceApp.alert(data.info,"提示")
                    }

                    $('#shedule .product_name').html(product_name);
                    var showDate = $('#showDate').fullCalendar({
                        header: {
                            left: 'prev',
                            center: 'title',
                            right: 'next'
                        },
                        // start: '2015-02-01',
                        // defaultDate: '2015-02-12',
                        aspectRatio: 1,
                        height: 500,
                        contentHeight: 300,
                        editable: true,
                        timezone: true,
                        //theme: true,
                        eventLimit: true, // allow "more" link when too many events
                        dayNamesShort: [
                            '日', '一', '二', '三', '四', '五', '六'
                        ],
                        events: data.data,
                        //events: [
                        //    {
                        //        title: 'All Day Event',
                        //        start: '2015-06-16',
                        //        end: '2015-06-18',
                        //        color: '#666666'
                        //    },
                        //],
                        monthNamesShort: [
                            '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'

                        ],
                        monthNames: [
                            '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'
                        ]
                    });
                    var html = templates.showSheduleTableTemplate({shows:data.data})

                    $('#shedule .shedule-table').html(html);
                    $('.deleteShedule').on('click',function(){
                        var id = $(this).data("id");
                        var name = $('#shedule .product_name').text()
                        pageCtrl.deleteShedule(id,name)
                    });
                    yunSpaceApp.hideIndicator()
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })

        },
        addSheduleData:function(product_id,product_name){
            yunSpaceApp.showIndicator();
            var startTime = $('#calendar-date-format1').val();
            var endTime = $('#calendar-date-format2').val();
            var des = $('#shedule-name').val();
            var url = appMain.apiRoot+ 'vendor/addProductScheduleOne';
            var color= $("#shedule-color").val();
            console.log(color);
            if(!startTime || !endTime){
                //yunSpaceApp.alert($("#shedule-color").val());
                yunSpaceApp.alert('开始日期和结束日期不能为空!','提示');
                yunSpaceApp.hideIndicator();
                return false
            }
            if(!color){
                yunSpaceApp.alert('亲，选个颜色提醒自己吧!','提示');
                yunSpaceApp.hideIndicator();
                return false;
            }
            $$.ajax({
                type:'post',
                dataType:'json',
                url:url,
                cache:false,
                timeout:requestTimeOut,
                data:{
                    'user_id':localUserId,
                    'product_id':product_id,
                    'start_time':startTime,
                    'end_time':endTime,
                    'des':des,
                    'color':color,
                    'access_token':accessToken,
                    'login_time':loginTime
                },
                success:function(data){
                    //console.log(data)
                    if(data.status == 1){
                        serviceView.router.reloadPage('shedule.html?id='+product_id+'&name='+product_name+'');
                    }else{
                        yunSpaceApp.alert(data.info,'提示');
                    }

                    yunSpaceApp.hideIndicator();
                },
                error: function (XHR, TS, ET) {
                    appMain.networkError('',XHR,TS,ET);
                }
            })

        },
        deleteShedule : function(id,name){
            yunSpaceApp.confirm("确认删除档期？","提示",function(){
                yunSpaceApp.showIndicator();
                var url = appMain.apiRoot+ 'vendor/deleteProductScheduleOne';
                $.ajax({
                    type:'post',
                    dataType:'json',
                    url:url,
                    cache:false,
                    timeout:requestTimeOut,
                    data:{
                        'id':id,
                        'user_id':localUserId,
                        'access_token':accessToken,
                        'login_time':loginTime
                    },
                    success:function(data){
                        console.log(data)
                        if(data.info == '删除成功'){
                            yunSpaceApp.alert(data.info,'提示');
                            serviceView.router.reloadPage('shedule.html?id='+pageCtrl.product_id+'&name='+name+'');
                        }else{
                            yunSpaceApp.alert(data.info,'提示');
                        }
                        yunSpaceApp.hideIndicator();
                    },
                    error: function (XHR, TS, ET) {
                        appMain.networkError('',XHR,TS,ET);
                    }
                })
            });

        },
        getPhoto:function(){
            navigator.camera.getPicture(pageCtrl.onPhotoDataSuccess, pageCtrl.onFail, {
                quality: 30,
                destinationType: Camera.DestinationType.DATA_URL,
            });
        },
        onPhotoDataSuccess:function(imageURI){
            var cameraImage = document.getElementById('image');
            cameraImage.style.display = 'block';
            cameraImage.src = imageURI;
        },
        onFail:function onFail(message) {
            alert('Failed because: ' + message);
        }
    }
    return pageCtrl
})
